
import config from './config'
import type {ParamsInterface} from '@/constrain/interface'
class Service{
	private api(params:ParamsInterface){
		
		return new Promise((resolve,reject):any=>{
			const {method,url,data} = params
			uni.showLoading({
				title:'正在加載中...'
			})
			type HeaderType = {
				'Content-Type':string,
				token?:string
			}
			const header:HeaderType = {
				'Content-Type':'application/json',
			}
			const token = uni.getStorageSync('token')
			token&& (header.token = token)
			uni.request({
				url:config.BASEURL + url,
				data,
				method,
				header,
				success:res=>{
					uni.hideLoading(),
					resolve(res.data)
				},
				fail:err=>{
					uni.hideLoading()
					reject(err)
				},
				complete:()=>{
					uni.hideLoading()
				}
			})
			
		})
	}
	get(options:ParamsInterface){
		options.method='GET'
		return this.api(options)
	}
	Post(options:ParamsInterface){
		options.method='POST'
		return this.api(options)
	}
	put(options:ParamsInterface){
		options.method='PUT'
		return this.api(options)
	}
	delete(options:ParamsInterface){
		options.method='DELETE'
		return this.api(options)
	}
}

export default Service